package model.os;

import static java.lang.Math.random;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;


public class OSProcess extends Thread {

	private int size;

	private List<Page> pages;

	private Color color;

	private boolean running;
	
	private int number;

	public OSProcess(int number) {
		this((int) (random() * 100 + 20), number);
	}

	public OSProcess(int size, int number) {
		this.number = number;
		this.size = size;
		initialize();
	}

	private void initialize() {
		pages = new ArrayList<Page>();
		Page page;
		for (int i = 0; i < size; i++) {
			page = new Page(i, this);
			pages.add(page);
		}
	}

	public void run() {
		running = true;
		while (running) {
			requestPages();
		}
		notifyAll();
	}

	public void stopThread() {
		running = false;
	}
	
	public boolean isRunning() {
		return running;
	}

	public void requestPages() {
		int requestSize = 1; // Random request size
		int page;
		int offset;
		Address virtualAddress;
		while (requestSize-- > 0) {
			page = (int) (random() * size); // Random page
			offset = (int) (random() * OS.PAGE_SIZE); // Random offset
			virtualAddress = new Address(page, offset);
			synchronized (OS.getInstance()) {
				OS.getInstance().getPage(this, virtualAddress);
			}
		}
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public List<Page> getPages() {
		return pages;
	}

	public void setPages(List<Page> pages) {
		this.pages = pages;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public Color getColor() {
		return color;
	}

	public void setRunning(boolean b) {
		running = b;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public int getNumber() {
		return number;
	}

}
